package com.hjyang.solr.util;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
import org.apache.solr.client.solrj.response.QueryResponse;

import java.io.File;
import java.net.URLEncoder;
import java.util.Date;
import java.util.UUID;

/**
 * @author hjyang11
 * @date 2019/10/29
 */
public class SolrTest {
    
    // 指定solr服务器的地址
    //public final static String SOLR_URL = "http://172.19.12.156:8983/solr/";
    public final static String SOLR_URL = "http://localhost:8983/solr/";
    // 指定solr core
    public final static String SOLR_CORE = "fireTune/";
    
    public static void main(String[] args) throws Exception {
        SolrClient server = SolrUtil.createSolrServer(SOLR_URL, SOLR_CORE);
        addDocs(server);
        QueryResponse rsp = server.query(new SolrQuery("*:*"));
        System.out.println(rsp);
        server.close();
    }
    
    public static void addDocs(SolrClient server) throws Exception {
        File file = new File("D:\\solr索引文件夹\\04.docx");
        ContentStreamUpdateRequest updateRequest = new ContentStreamUpdateRequest("/update/extract");
        updateRequest.setMethod(SolrRequest.METHOD.POST);
        updateRequest.addFile(file, TestCreate.getFileContentType(file.getName()));
        updateRequest.setParam("literal.id", URLEncoder.encode(UUID.randomUUID().toString().replaceAll("-","")));
        updateRequest.setParam("literal.accident_id", "11111114");
        updateRequest.setParam("literal.accident_name", "11111114");
        updateRequest.setParam("literal.sggk_qhsj", "11111114");
        updateRequest.setParam("literal.sggk_qhdd", "11111114");
        updateRequest.setParam("literal.qhjz_qhzt", "11111114");
        updateRequest.setParam("literal.sggk_ssrs", "11111114");
        updateRequest.setParam("literal.sggk_swrs", "11111114");
        updateRequest.setParam("literal.sggk_ghmj", "11111114");
        updateRequest.setParam("literal.org_code", "11111114");
        updateRequest.setParam("literal.gmt_create", "11111114");
        updateRequest.setParam("literal.accident_file_name", "11111114");
        updateRequest.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
        updateRequest.setParam("fmap.content", "accident_file_content");
        server.request(updateRequest);
    }
}
